package com.example.studentmanage.controller;

import com.example.studentmanage.dao.CourseDao;
import com.example.studentmanage.dao.StudentDao;
import com.example.studentmanage.pojo.Course;
import com.example.studentmanage.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.Iterator;

@Controller
public class StudentController {

    @Autowired
    StudentDao studentDao;

    @Autowired
    CourseDao courseDao;

    @RequestMapping("/hello/stu")
    public String helloStu(Model model){
        Collection<Student> students=studentDao.getAllStudent();
        model.addAttribute("student",students);
        return "student";
    }

    @RequestMapping("/hello/show")
    public String helloShow(Model model){
        Collection<Student> students=studentDao.getAllStudent();
        Collection<Course> courses=courseDao.getAllCourse();

        Iterator<Student> s = students.iterator();
        Iterator<Course> c = courses.iterator();

        while(s.hasNext()){
            Course cour = c.next();
            Student stu = s.next();
            stu.setCourse(cour);
//            System.out.println(ele);
        }
//        for(Student s:students){
//            for(Course c:courses){
//                s.setCourse(c);
//
//                break;
//            }
//        }
        model.addAttribute("stu",students);
        return "show";
    }


    @RequestMapping("/upStu/{id}")
    public String upStudent(@PathVariable("id")Integer id,Model model){
            Student student=studentDao.getStudentById(id);
            System.out.println(id);
            model.addAttribute("stu",student);
            return "update";
    }

    @PostMapping("/updateStu")
    public String update(Student student){
        studentDao.save(student);
        return "redirect:/hello/stu";
    }

}
